console.log(obj)
var minlen;

var app = new Vue({
    el:'#content',
    data:{
        list:obj,
        address_name:'',
        address_tel:'',
        content:'',
        listLen:0,
        listMoney:0,
        statusLocation:{},
        ifdizhi:false,
        formdata:{
            name: '',
            tel: '',
            province: '',
            city: '',
            area: '',
            status:0,
            address: ''
        }
    },
    methods: {
        ok(){
            this.formdata.name = this.address_name
            this.formdata.tel = this.address_tel
            this.Split()
            if(minlen[2] != ""){
                this.formdata.province = minlen[0] + '省'
                this.formdata.city = minlen[1] + '市'
                this.formdata.area = minlen[2]
            }else if(minlen[2] == ""){
                this.formdata.province = minlen[0] + '市'
                this.formdata.city = minlen[1]
                this.formdata.area = minlen[2]
            }
            this.formdata.address = this.content
        },
        Split(){
            var maxlen = $('#sel_city')[0].innerHTML
            minlen = maxlen.split("-")
            console.log(minlen)
        },
        saveLocation(){
            this.ok()
            const data = new URLSearchParams()
            for(let key in this.formdata){
                console.log(this.formdata[key])
                data.append(key, this.formdata[key])
            }
            data.append('address_id', '')
            axios.post('user.php?act=update_or_insert_address&ajax=true',data).then(e =>{
                if(e.data.code == 1){
                    alert('保存成功')
                    this.ifdizhi = false
                }
            })
        },
        go2pay(){
            console.log(this.formdata)
            const data = new URLSearchParams()
            const goods = this.list.map(e =>({
                iscomment:0,
                id: e.goods_id,
                price: e.goods.price,
                num: e.goods_num,
                name: e.goods.title
            }))
            console.log(goods)
            const address = {
                name: this.formdata.name,
                tel: this.formdata.tel,
                address: this.formdata.province + this.formdata.city + this.formdata.area + this.formdata.address
            }
            console.log(address)
            console.log(this.listMoney)
            data.append('goods', JSON.stringify(goods))
            data.append('address', JSON.stringify(address))
            data.append('total', this.listMoney)
            data.append('remarks', 'I am remarks')
            axios.post('user.php?act=add2order&ajax=true', data).then(r => {
                if (r.data.code == 1) {
                    alert('操作成功')
                    // this.getAllData()
                    window.location.href = 'user.php?act=order_success&order_id='+r.data.order_id
                } else {
                    alert('操作失败')
                    // this.$message.error('兄弟，醒醒，没钱还想下单？')
                }
            })
        },
        close(){
            
        },
        sumMoney(){
            var listlen = 0;
            var listmoney = 0;
            for(let i of obj){
                listlen = listlen + parseInt(i.goods_num)
                listmoney = listmoney + parseFloat(i.goods.price)*parseInt(i.goods_num)
            }
            console.log(listlen)
            console.log(listmoney)
            this.listLen = listlen
            this.listMoney = listmoney.toFixed(2)
        },
        getStatuslocation(){
            axios.get('user.php?act=getstatusaddress').then(r =>{
                this.formdata.name = r.data.name
                this.formdata.tel = r.data.tel
                this.formdata.province = r.data.province
                this.formdata.city = r.data.city
                this.formdata.area = r.data.area
                this.formdata.address = r.data.address
                this.address_name = r.data.name
                this.address_tel = r.data.tel
                this.content = r.data.address
                $('#sel_city').html(r.data.province + r.data.city + r.data.area)
            })
        }
    },
    created() {
        this.sumMoney()
        this.getStatuslocation()
    },
})